package com.msg.xml;

import java.util.ArrayList;
import java.util.List;

/**
 * This class will make use of Capacity Manipulator, Available Seats Manipulator and CompSeats Manipulator to 
 * read the capacity information from XML and save the data in Capacity table
 * @author jeson9603
 *
 */
public class Capacity_Controller extends Table_Manipulator {

	private List<CapacityRecord> records = new ArrayList<CapacityRecord>();
	private Capacity_Manipulator capManipulator;
	private AvaSeats_Manipulator avaManipulator;
	private CompSeats_Manipulator compManipulator;
	
	
	public Capacity_Controller(Property prop) {
		super(prop);
		capManipulator = new Capacity_Manipulator(prop);
		avaManipulator = new AvaSeats_Manipulator(prop);
		compManipulator = new CompSeats_Manipulator(prop);
	}
	/**
	 * call the getSepcificData in Capacity_Manipulator, AvaSeats_Manipulator, CompSeats_Manipulator to get the 
	 * capacity data, available seats data and comp seats data from XML
	 */
	@Override
	protected String getSpecificData() {
		capManipulator.getSpecificData();
		avaManipulator.getSpecificData();
		compManipulator.getSpecificData();
		return null;
	}

	/**
	 * create a list of capacity records and continuously use Capacity Manipulator, AvaSeats Manipulator and
	 * CompSeats Manipulator to read data from XML and save it in the records list
	 */
	private void populateRecords() {
		//get capacity data by using Capacity Manipulator
		capManipulator.pushData(records);
		capManipulator.update();
		//get available seats data by using AvaSeats Manipulator
		avaManipulator.pushData(records);
		avaManipulator.update();
		//get comp seats data by using CompSeats Manipulator
		compManipulator.pushData(records);
		compManipulator.update();
	}
	
	/**
	 * Add a new capacity record to the DB
	 * @param request : request contains id
	 * @param record : capacity record contains the seats information
	 */
	private void addCapacityRecord(DBRequest request, CapacityRecord record) {
		request.resultCol.remove("Capacity");
		request.entries.add("Capacity");
		request.entries.add("Comp_Seats");
		request.entries.add("Available_Seats");
		request.data.add(record.getCapacity());
		request.data.add(record.getCompSeats());
		request.data.add(record.getAvaSeats());
		
		DBhandler.insertData("capacity", request);
	}
	
	/**
	 * update the capacity records with given seats data.
	 * @param request : request contains id
	 * @param record : capacity record contains the seats information
	 */
	private void updateCapacityRecord(DBRequest request, CapacityRecord record) {
		request.resultCol.add("Comp_Seats");
		request.resultCol.add("Available_Seats");
		request.resultColData.add(record.getCapacity());
		request.resultColData.add(record.getCompSeats());
		request.resultColData.add(record.getAvaSeats());
		DBhandler.updateData("capacity", request);
	}
	
	/**
	 * First using the populate Records method to get data from XML to list of records
	 * After then, it will check if each record is valid in database, (contains such event code in event table)
	 * If this event code in event table, add this new capacity record in capacity table
	 */
	@Override
	protected String update() {
		populateRecords();
		for(int i = 0; i < records.size(); i++) {
			CapacityRecord record = records.get(i);
			String eventId = prop.getEVENT_ID();
			//if such event id exist in event table, we need then retrieve the ticket type id and ticket level id	
			String ticketTypeId = TableUtility.getTicketTypeId(record.getTicketType());
			String ticketLvId = TableUtility.getTicketLvID("" + record.getTicketLevel());
			if((ticketTypeId != null) && (ticketLvId != null)) {
				DBRequest request = new DBRequest();
				request.resultCol.add("Capacity");
				request.entries.add("Event_Id");
				request.entries.add("Ticket_Type_Id");
				request.entries.add("Ticket_Level_Id");
				request.data.add(eventId);
				request.data.add(ticketTypeId);
				request.data.add(ticketLvId);
				//if the capacity doesn't have this record, adding it in capacity table
				if(!DBhandler.hasData("capacity", request))
					addCapacityRecord(request, record);
				else
					updateCapacityRecord(request, record);
			}
		}
		return null;
	}

	/**
	 * Empty Stub, since it is a controller class, so it doesn't need get data from outside
	 */
	@Override
	protected void pushData(Object o) {
		// TODO Auto-generated method stub
	}

}
